package com.yy.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yy.common.annotation.SysLog;
import com.yy.model.MetaEntity;
import com.yy.service.MetaService;
import com.yy.utils.PageUtils;
import com.yy.utils.Query;
import com.yy.utils.ResponseEntity;
import com.yy.validator.ValidatorUtils;
import com.yy.validator.group.AddGroup;
import com.yy.validator.group.UpdateGroup;


/**
 * 网页信息
 * 
 * @author yy
 * @email 
 * @date 2017-07-25 17:04:00
 */
@RestController
@RequestMapping("meta")
public class MetaController {
	@Autowired
	private MetaService metaService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("meta:list")
	public ResponseEntity list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<MetaEntity> metaList = metaService.queryList(query);
		int total = metaService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(metaList, total, query.getLimit(), query.getPage());
		
		return ResponseEntity.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("meta:info")
	public ResponseEntity info(@PathVariable("id") Integer id){
		MetaEntity meta = metaService.queryObject(id);
		
		return ResponseEntity.ok().put("meta", meta);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存网页信息")
	@RequestMapping("/save")
	@RequiresPermissions("meta:save")
	public ResponseEntity save(@RequestBody MetaEntity meta){
		ValidatorUtils.validateEntity(meta, AddGroup.class);
		
		if (!StringUtils.isBlank(meta.getContent())) {
			try {
				meta.setContent(URLDecoder.decode(meta.getContent(),"UTF-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
		metaService.save(meta);
		
		return ResponseEntity.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改网页信息")
	@RequestMapping("/update")
	@RequiresPermissions("meta:update")
	public ResponseEntity update(@RequestBody MetaEntity meta){
		ValidatorUtils.validateEntity(meta, UpdateGroup.class);
		
		if (!StringUtils.isBlank(meta.getContent())) {
			try {
				meta.setContent(URLDecoder.decode(meta.getContent(),"UTF-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
		metaService.update(meta);
		
		return ResponseEntity.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除网页信息")
	@RequestMapping("/delete")
	@RequiresPermissions("meta:delete")
	public ResponseEntity delete(@RequestBody Integer[] ids){
		metaService.deleteBatch(ids);
		
		return ResponseEntity.ok();
	}
	
}
